package cate.game.server.service;

import cate.common.table.EContext;
import cate.common.table.common.*;
import cate.game.ManyGame;
import cate.game.table.GameTableContext;
import cate.game.table.TableManager;
import cate.game.util.GameTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class CheckService {

    @Value("${cate.checkServiceOn:false}")
    private boolean checkServiceOn;

    /** 执行静态检查 */
    public void execute(ManyGame games, TableManager table, GameTools tools) {
        log.info("checkServiceOn={}, 生产环境该值应当为false", checkServiceOn);
        if(!checkServiceOn) {
            return;
        }
        checkNameExcel(games, tools);
    }

    private void checkNameExcel(ManyGame games, GameTools tools) {
        StringBuilder sb = new StringBuilder();
        sb.append("不符合SHZY核心Value观の名字：\r\n");
        EContext ctx = new GameTableContext(games.config.excelPath, games.configFromConsole.excelMode);
        NameExcel excel = new NameExcel();
        excel.setContext(ctx);
        excel.load();
        log.info("开始进行检测名字库");
        int total = 0;
        for(NameXingRow row : excel.xing.list) {
            if(!tools.sensitiveWords.legal(row.value)) {
                sb.append(excel.getFileName()+"/"+excel.xing.getSheetName()+"/ID="+row.id+"("+row.value+")\r\n");
                total++;
            }
        }
        log.info("检测完成 {} total={}", excel.xing.getSheetName(), total);
        for(NameMingRow row : excel.ming.list) {
            if(!tools.sensitiveWords.legal(row.value)) {
                sb.append(excel.getFileName()+"/"+excel.ming.getSheetName()+"/ID="+row.id+"("+row.value+")\r\n");
                total++;
            }
        }
        log.info("检测完成 {} total={}", excel.ming.getSheetName(), total);
        for(NameFullRow row : excel.full.list) {
            if(!tools.sensitiveWords.legal(row.value)) {
                sb.append(excel.getFileName()+"/"+excel.full.getSheetName()+"/ID="+row.id+"("+row.value+")\r\n");
                total++;
            }
        }
        log.info("检测完成 {} total={}", excel.full.getSheetName(), total);
        for(NameGuildRow row : excel.guild.list) {
            if(!tools.sensitiveWords.legal(row.value)) {
                sb.append(excel.getFileName()+"/"+excel.guild.getSheetName()+"/ID="+row.id+"("+row.value+")\r\n");
                total++;
            }
        }
        log.info("检测完成 {} total={}", excel.guild.getSheetName(), total);
        log.error("有如下这些:\r\n{}", sb);
    }
}
